/*----------------------------------------------------------------------
  - File name      : STC8Ax_EEPROM.c
  - Author         : Quark Team
  - Update date    : 2020-08-30                   
  -	Copyright      : Gevico Electronic studio   
  - Module comments: EEPROM module.
-----------------------------------------------------------------------*/
/*-----------------------------------------------------------------------
|                               INCLUDES                                |
-----------------------------------------------------------------------*/
#include "STC8Ax_EEPROM.h"
#include "intrins.h"
/*-----------------------------------------------------------------------
|                                 DATA                                  |
-----------------------------------------------------------------------*/
/* None */
/*-----------------------------------------------------------------------
|                               FUNCTION                                |
-----------------------------------------------------------------------*/

/*--------------------------------------------------------
| @Description: EEPROM working wait time function        |
| @param      : None                                     |
| @return     : None                                     |
--------------------------------------------------------*/

static void EEPROM_WT_Time(void)
{	
	IAP_CONTR &= 0xF8;
	if (SYSCLK_FRE >= 30000000UL) IAP_CONTR |= 0x00;	
	else if ((SYSCLK_FRE >= 24000000UL) && (SYSCLK_FRE < 30000000UL)) IAP_CONTR |= 0x01;
	else if ((SYSCLK_FRE >= 20000000UL) && (SYSCLK_FRE < 24000000UL)) IAP_CONTR |= 0x02;
	else if ((SYSCLK_FRE >= 12000000UL) && (SYSCLK_FRE < 20000000UL)) IAP_CONTR |= 0x03;
	else if ((SYSCLK_FRE >=  6000000UL) && (SYSCLK_FRE < 12000000UL)) IAP_CONTR |= 0x04;
	else if ((SYSCLK_FRE >=  3000000UL) && (SYSCLK_FRE <  6000000UL)) IAP_CONTR |= 0x05;
	else if ((SYSCLK_FRE >=  2000000UL) && (SYSCLK_FRE <  3000000UL)) IAP_CONTR |= 0x06;
	else if ((SYSCLK_FRE >=  1000000UL) && (SYSCLK_FRE <  2000000UL)) IAP_CONTR |= 0x07;
}

/*--------------------------------------------------------
| @Description: EEPROM init function                     |
| @param      : FUNSTATE State                           |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE EEPROM_Ctrl(FUNSTATE State)
{
	IAP_CONTR = (IAP_CONTR & 0x7F) | (State << 7);
    EEPROM_WT_Time();
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: EEPROM read byte function                |
| @param      : uint16 Addr,uint8 *Byte                  |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE EEPROM_Read_Byte(uint16 Addr,uint8 *Byte)
{
	IAP_CMD = 1;  //Set IAP read command
	IAP_ADDRH = Addr >> 8; 	//Set IAP high address
	IAP_ADDRL = Addr; 		//Set IAP low address
	IAP_TRIG  = 0x5A; 		//Write trigger command (0x5a)
	IAP_TRIG  = 0xA5; 		//Write trigger command (0xa5)
	*Byte = IAP_DATA; 		//Read IAP data	
	_nop_(); 
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: EEPROM read buff function                |
| @param      : uint16 Addr,uint8 *Str, uint8 Len        |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE EEPROM_Read_Buff(uint16 Addr,uint8 *Str,uint8 Len)
{
	while(Len--)
	{
		EEPROM_Read_Byte(Addr++,Str++);
	}
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: EEPROM write byte function               |
| @param      : uint16 Addr,uint8 Byte                   |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE EEPROM_Write_Byte(uint16 Addr,uint8 Byte)
{
	IAP_CMD = 2;  //Set IAP write command
	IAP_ADDRH = Addr >> 8; 	//Set IAP high address
	IAP_ADDRL = Addr; 		//Set IAP low address
	IAP_DATA =  Byte; 		//Read IAP data	
    IAP_TRIG  = 0x5A; 		//Write trigger command (0x5a)
	IAP_TRIG  = 0xA5; 		//Write trigger command (0xa5)
	_nop_(); 
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: EEPROM write string function             |
| @param      : uint16 Addr,const uint8 *Str             |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE EEPROM_Write_Str(uint16 Addr,const uint8 *Str)
{
	while(*Str)
	{
		EEPROM_Write_Byte(Addr++,*Str++);
	}
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: EEPROM write buff function               |
| @param      : uint16 Addr,const uint8 *Str, uint8 Len  |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE EEPROM_Write_Buff(uint16 Addr,const uint8 *Str,uint8 Len)
{
	while(Len--)
	{
		EEPROM_Write_Byte(Addr++,*Str++);
	}
	return FSC_SUCCESS;
}

/*--------------------------------------------------------
| @Description: EEPROM erase page function               |
| @param      : uint16 Addr                              |
| @return     : FSCSTATE : Success / erro                |
--------------------------------------------------------*/

FSCSTATE EEPROM_Erase_Page(uint16 Addr)
{
	IAP_CMD = 3;  //Set IAP write command
	IAP_ADDRH = Addr >> 8; 	//Set IAP high address
	IAP_ADDRL = Addr; 		//Set IAP low address
	IAP_TRIG = 0x5A; 		//Write trigger command (0x5a)
	IAP_TRIG = 0xA5; 		//Write trigger command (0x5a)	
	_nop_(); 
	return FSC_SUCCESS;
}

/*-----------------------------------------------------------------------
|          END OF FLIE        (C) COPYRIGHT Gevico Electronics          | 
-----------------------------------------------------------------------*/



